package com.chookss.home.tuwen;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.Token;
import com.chookss.home.entity.TuwenEntity;
import com.chookss.home.studyPlan.StudyPlanAddActivity;
import com.chookss.mine.entity.MyViedeoEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.MyImageSpan;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class TuwenAdapter extends BaseQuickAdapter<TuwenEntity, BaseViewHolderEx> {
    private Context context;
    private int height, width;
    private int type=1; //0全部图文1是我的图文2是选择图文
    private int index=-1; //选择图文下标

    public TuwenAdapter(int layoutResId, Context context, @Nullable List<TuwenEntity> data,int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 48)) / 3;
        height = width * 65 / 102;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, TuwenEntity item) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        RelativeLayout rlImgChoice = helper.getView(R.id.rlImgChoice);
        ImageView ivChoice = helper.getView(R.id.ivChoice);
        View line = helper.getView(R.id.line);
        TextView tvName = helper.getView(R.id.tvName);
        TextView tvDate = helper.getView(R.id.tvDate);
        LinearLayout llOneImg = helper.getView(R.id.llOneImg);
        LinearLayout llThreeImg = helper.getView(R.id.llThreeImg);
        ImageView ivImg = helper.getView(R.id.ivImg);
        ImageView ivPic1 = helper.getView(R.id.ivPic1);
        ImageView ivPic2 = helper.getView(R.id.ivPic2);
        ImageView ivPic3 = helper.getView(R.id.ivPic3);
        View placeholderView = helper.getView(R.id.placeholderView);
        View spaceView = helper.getView(R.id.spaceView);
        //两个view只能同时存在一个
        LinearLayout llContent = helper.getView(R.id.llContent);
        if(type==2){
            rlImgChoice.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) line.getLayoutParams();
            params.setMargins(Utils.dip2px(context,30),0,0,0);
            line.setLayoutParams(params);
            if(index==helper.getItemPosition()){
                ivChoice.setBackgroundResource(R.drawable.iv_choice_select_16);
            }else{
                ivChoice.setBackgroundResource(R.drawable.iv_choice_16);
            }
        }else{
            rlImgChoice.setVisibility(View.GONE);
        }
        String title = item.getTitle().trim().replace("\r|\n", "");
        LinearLayout.LayoutParams llOneImgParams = (LinearLayout.LayoutParams) llOneImg.getLayoutParams();
        LinearLayout.LayoutParams ivPic1Params = (LinearLayout.LayoutParams) ivPic1.getLayoutParams();
        LinearLayout.LayoutParams ivPic2Params = (LinearLayout.LayoutParams) ivPic2.getLayoutParams();
        LinearLayout.LayoutParams ivPic3Params = (LinearLayout.LayoutParams) ivPic3.getLayoutParams();
        LinearLayout.LayoutParams llContentParams = (LinearLayout.LayoutParams) llContent.getLayoutParams();
//            (0 暂存 1提交 2通过 3不通过 )
        if ("0".equals(item.getStatusCd())) {
            //暂存
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.iv_zan_save);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        }else if ("1".equals(item.getStatusCd())) {
            //审核中
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_red_shen);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        } else if ("3".equals(item.getStatusCd())) {
            //审核未通过
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_red_tui);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        }else {
            tvTitle.setText(title);
        }
        tvName.setText(item.getCreateEmployeeName());
        tvDate.setText(Utils.getAccurateDate(item.getCreateTime()));
        if (item.getPictureList().size() > 0) {
            if (item.getPictureList().size() < 3) {
                llOneImg.setVisibility(View.VISIBLE);
                llThreeImg.setVisibility(View.GONE);
                llOneImgParams.height = height;
                llOneImgParams.width = width;
                llOneImg.setLayoutParams(llOneImgParams);
                llContentParams.height = height;
                placeholderView.setVisibility(View.VISIBLE);
                spaceView.setVisibility(View.GONE);
                llContent.setLayoutParams(llContentParams);
                GlideUtils.loadCustomRound(context,item.getPictureList().get(0), ivImg, 4);
            } else {
                llOneImg.setVisibility(View.GONE);
                llThreeImg.setVisibility(View.VISIBLE);
                ivPic1Params.height = height;
                ivPic1Params.width = width;
                ivPic1.setLayoutParams(ivPic1Params);
                ivPic2Params.height = height;
                ivPic2Params.width = width;
                ivPic2.setLayoutParams(ivPic2Params);
                ivPic3Params.height = height;
                ivPic3Params.width = width;
                ivPic3.setLayoutParams(ivPic3Params);
                llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                placeholderView.setVisibility(View.GONE);
                spaceView.setVisibility(View.VISIBLE);
                llContent.setLayoutParams(llContentParams);
                GlideUtils.loadCustomRound(context, item.getPictureList().get(0), ivPic1, 4);
                GlideUtils.loadCustomRound(context, item.getPictureList().get(1), ivPic2, 4);
                GlideUtils.loadCustomRound(context, item.getPictureList().get(2), ivPic3, 4);
            }
        } else {
            llOneImg.setVisibility(View.GONE);
            llThreeImg.setVisibility(View.GONE);
            placeholderView.setVisibility(View.GONE);
            spaceView.setVisibility(View.VISIBLE);
            llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            llContent.setLayoutParams(llContentParams);

        }
        helper.getItemView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(type==1&&!"1".equals(item.getStatusCd())){
                    //个人情况下 审核中不能删除，其他都可以
                    deleteTuwenDialog(item.getId());
                }
                return true;
            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addressType = "";
                if (Urls.baseUrl.contains("dev_kmer")) {
                    addressType = "1";
                } else {
                    addressType = "0";
                }
                String url = Urls.tuwenDetail+"mainCode=" + item.getMainCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                Intent intent = new Intent(context, WebCommonActivity.class);
                intent.putExtra("url", url);
                intent.putExtra("title", "详情");
                if ("0".equals(item.getStatusCd())) {
                    intent.putExtra("isTuwen", 2);
                }else if ("1".equals(item.getStatusCd())||"3".equals(item.getStatusCd())) {
                }else{
                    intent.putExtra("isTuwen", 3);
                }
                intent.putExtra("waterMark", "13");
                intent.putExtra("url", url);
                intent.putExtra("createEmployeeName", item.getCreateEmployeeName());
                intent.putExtra("newsTitle", item.getTitle());
                intent.putExtra("resourceId", item.getMainCode());
                intent.putExtra("newsContent", "");
                if (item.getPictureList().size() > 0) {
                    if (Utils.isNull(item.getPictureList().get(0))) {
                        intent.putExtra("picUrl", item.getPictureList().get(0));
                    } else {
                        intent.putExtra("picUrl", item.getPictureList().get(0));
                    }
                }else{
                    intent.putExtra("picUrl","");
                }
                context.startActivity(intent);
            }
        });

    }
    public void setChoiceIndex(int num){
        index=num;
    }
    private void deleteTuwenDialog(String id){
        new android.app.AlertDialog.Builder(context)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        deleteTuwen(id);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setMessage("确定要删除图文么?")
                .setTitle("提示")
                .show();

    }
    private void deleteTuwen(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<String>>() {
        }.getType(), null, Urls.deleteByParameter, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                MyToast.show("删除成功");
                HashMap<String, String> map = new HashMap<>();
                map.put("deleteTuwenId", id);
                EventBus.getDefault().post(new MyEvent(map));
            }
            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
}
